{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7eb49aae",
   "metadata": {},
   "source": [
    "--------\n",
    "\n",
    
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01d02ba4",
   "metadata": {},
   "source": [
    "## 本周内容\n",
    "### [集合](https://docs.python.org/3/tutorial/datastructures.html#sets)\n",
    "\n",
    "- 1. 集合是无序的\n",
    "- 2. 集合是不重复的\n",
    "- 3. 集合的一般方法\n",
    "> 增加\n",
    "> 删除\n",
    "> in查询\n",
    ">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "24332d51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{18, 24, 25, 90}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_age = [18,24,25,18,24,90,18]\n",
    "\n",
    "# 用集合获取列表中第一次出现的各值\n",
    "\n",
    "no_duplicate_age_set = set(list_age)\n",
    "no_duplicate_age_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "42a6c52f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'orange', 'apple', 'pear'}\n"
     ]
    }
   ],
   "source": [
    "# 官方文档的例子\n",
    "\n",
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "\n",
    "# 1. 将会把重复的值移除\n",
    "print(basket)     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fc9867b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 2. 可用 in 查询关系成员\n",
    "print('orange' in basket)\n",
    "print('crabgrass' in basket)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a961f1f",
   "metadata": {},
   "source": [
    "### 集合的一般方法\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0f7bb7f",
   "metadata": {},
   "source": [
    "#### 集合创建的两种一般方法\n",
    "    \n",
    "1. set(list)\n",
    "2. 直接按照集合的结构创建：{value1,value2,value3,...}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b7f77628",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ae94177",
   "metadata": {},
   "source": [
    "#### 增加的一般方法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "94ac4608",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 3, 4, 7, 8, 66, 99}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_1.add(99)\n",
    "set_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bd2ad3bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "add(...)\n",
      "    Add an element to a set.\n",
      "    \n",
      "    This has no effect if the element is already present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set.add)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "918ea936",
   "metadata": {},
   "source": [
    "#### 删除的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d57a2c45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "pop(...)\n",
      "    Remove and return an arbitrary set element.\n",
      "    Raises KeyError if the set is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4d42fb43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{3, 4, 7, 8, 66, 99}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机删除一个元素\n",
    "set_1.pop()\n",
    "set_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c909128c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "remove(...)\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set.remove)  # 如果集合里没有这个元素则会报错，所以一般 remove() 会和 in 查询一起使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9ea0c169",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{3, 4, 7, 8, 99}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定删除一个元素\n",
    "if 66 in set_1:\n",
    "    set_1.remove(66)\n",
    "\n",
    "set_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "25cf9152",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "discard(...)\n",
      "    Remove an element from a set if it is a member.\n",
      "    \n",
      "    If the element is not a member, do nothing.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set.discard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c69cdbf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{3, 4, 7, 8, 99}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定删除一个元素，但如果里面没有元素将没有变化，也不会报错\n",
    "set_1.discard(2)\n",
    "set_1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8e6ab09",
   "metadata": {},
   "source": [
    "#### 集合的运算\n",
    "##### 集合的交集\n",
    "1. 使用集合的方法：set.intersection(set_2)\n",
    "2. set_1 & set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0fca5fa6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6, 7}\n",
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_2 = set([1,2,5,6,7,2,3,2,4,4])\n",
    "set_3 = set([0,1,66,3,4,7,8])\n",
    "print(set_2)\n",
    "print(set_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e8d2e292",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "intersection(...)\n",
      "    Return the intersection of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in both sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set.intersection)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9c98ac49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3, 4, 7}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_2.intersection(set_3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdf8b0b4",
   "metadata": {},
   "source": [
    "##### 集合的并集\n",
    "1. 使用集合的方法：set.union(set_1)\n",
    "2. 使用逻辑运算符：set_1 | set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "303bc5ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "union(...)\n",
      "    Return the union of sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in either set.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set.union)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f9964e82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 2, 3, 4, 5, 6, 7, 8, 66}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一\n",
    "set_2.union(set_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c66f01e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 2, 3, 4, 5, 6, 7, 8, 66}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法二\n",
    "set_2 | set_3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1554e69e",
   "metadata": {},
   "source": [
    "##### 集合的差集\n",
    "1. 使用集合的方法：set_1.difference(set_2)\n",
    "2. 使用逻辑运算符：set_1 - set_2\n",
    "3. **注意：差集类似于减法，所以先后顺序很重要**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "81c8891e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "difference(...)\n",
      "    Return the difference of two or more sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in this set but not the others.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set.difference)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ceaaeff0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 5, 6}\n",
      "{0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "# 方法一\n",
    "print(set_2.difference(set_3))\n",
    "print(set_3.difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5b4301a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{2, 5, 6}\n",
      "{0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "# 方法二\n",
    "print(set_2 - set_3)\n",
    "print(set_3 - set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "297ea082",
   "metadata": {},
   "source": [
    "##### 对称差集\n",
    "1. 使用集合的方法：set_1.symmetric_difference(set_2)\n",
    "2. 使用逻辑运算符：(set_1 - set_2) | (set_2 - set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "07311c2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method_descriptor:\n",
      "\n",
      "symmetric_difference(...)\n",
      "    Return the symmetric difference of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in exactly one of the sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set.symmetric_difference)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "6b8e28cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 2, 5, 6, 8, 66}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一\n",
    "set_2.symmetric_difference(set_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e59f873e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 2, 5, 6, 8, 66}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法二\n",
    "(set_2 - set_3) | (set_3 - set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "119ebc2a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
